<template>
  <div>
    <v-btn
      :disabled="!isDocumentQualitySelected"
      class="text-capitalize"
      outlined
      @click="dialog = true"
    >
      质检驳回
    </v-btn>
    <v-dialog v-model="dialog" width="800">
      <confirm-form
        title="质检驳回"
        message="你确定要驳回这些标注吗？"
        @ok="handleDeleteDocument()"
        @cancel="dialog = false"
        buttonTrueText="确定"
        buttonFalseText="取消"
      />
    </v-dialog>
  </div>
</template>

<script>
import { mapState, mapGetters, mapActions, mapMutations } from 'vuex'
import DocumentService from '@/services/document.service'
import ConfirmForm from '@/components/organisms/utils/ConfirmForm'

export default {
  components: {
    ConfirmForm
  },
  // async fetch() {
  //   const data = await this.getQualityItems({
  //     projectId: this.$route.params.id,
  //     ...this.$route.query
  //   })
  //   console.log(data)
  //   this.table = this.qualityItems
  // },
  data() {
    return {
      dialog: false,
      table: [],
      data: [],
      arr: [],
      row: []
    }
  },
  watch: {
    qualitySelected: {
      handler(val) {
        this.row = val
      },
      immediate: true
    }
  },

  computed: {
    ...mapState('user', ['userId']),
    ...mapState('documents', ['qualitySelected']),
    ...mapGetters('documents', ['isDocumentQualitySelected'])
  },

  methods: {
    ...mapMutations('documents', ['resetQuatilySelected']),
    ...mapActions('documents', [
      'getDocumentList',
      'getQualityItems',
      'updateDocument'
    ]),
    ...mapActions('documents', ['deleteDocument']),

    async handleDeleteDocument() {
      this.row.forEach((item) => {
        this.arr.push(item.documentId)
      })
      const data = await DocumentService.qualityRollBack({
        projectId: this.$route.params.id,
        documentId: this.arr,
        userId: localStorage.getItem('userId'),
        opType: 1
      })
      this.row = []
      this.arr = []
      this.resetQuatilySelected()
      const table = await DocumentService.getQualityItems({
        projectId: this.$route.params.id,
        q: '',
        limit: 10,
        offset: 0,
        startTime: '',
        endTime: ''
      })
      console.log(table)
      console.log(data)
      this.$emit('refresh', table.data.results)
      this.$emit('erro', data.data.code)
      console.log(data)
      this.dialog = false
    }
  }
}
</script>
